const path = require('path')
const webpack = require('webpack')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']
const antLessVarConfig = require('./ant.less.var.config')

function resolve(dir) {
  return path.join(__dirname, dir)
}
const Timestamp = new Date().getTime() // 当前时间为了防止打包缓存不刷新，所以给每个js文件都加一个时间戳
// vue.config.js
module.exports = {
  // 生产环境去掉map文件 减小打包大小
  productionSourceMap: false,
  // 测试和生产都需要改变静态文件地址
  publicPath: process.env.NODE_ENV !== 'development' ? './' : './',
  configureWebpack: {
    output: {
      // 输出重构  打包编译后的 文件名称  【模块名称.时间戳】
      filename: `[name].${Timestamp}.js`,
      chunkFilename: `[name].${Timestamp}.js`
    },
    plugins: [
      new webpack.IgnorePlugin({
        resourceRegExp: /^\.\/locale$/,
        contextRegExp: /moment$/
      }),
      new CompressionWebpackPlugin({
        algorithm: 'gzip',
        test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
        threshold: 10240,
        minRatio: 0.8
      })
    ],
    optimization: {
      splitChunks: {
        chunks: 'all',
        automaticNameDelimiter: '~',
        name: 'desayChunks',
        cacheGroups: {
          // 第三方库抽离
          vendor: {
            name: 'vendor',
            test: /[\\/]node_modules[\\/]/,
            priority: -10,
            minChunks: 1,
            maxInitialRequests: 10,
            maxSize: 2201600
          },
          echarts: {
            name: 'echarts',
            test: /[\\/]node_modules[\\/]echarts[\\/]/,
            minChunks: 1,
            maxInitialRequests: 5,
            maxSize: 2201600
          },
          'ant-design-vue': {
            name: 'antd',
            test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/
          },
          common: {
            chunks: 'all',
            test: /[\\/]src[\\/]js[\\/]/,
            name: 'common',
            minChunks: 2,
            maxInitialRequests: 5,
            minSize: 1024,
            priority: 80
          },
          styles: {
            name: 'styles',
            test: /\.(le|sa|sc|c)ss$/,
            chunks: 'all',
            enforce: true
          }
        }
      }
    }
  },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'less',
      patterns: [path.resolve(__dirname, 'src/styles/commonLess/variables.less')]
    }
  },
  chainWebpack: (config) => {
    // 设置别名
    config.resolve.alias
      .set('@$', resolve('src'))
      .set('@api', resolve('src/api'))
      .set('@assets', resolve('src/assets'))
      .set('@comp', resolve('src/components'))
      .set('@views', resolve('src/views'))
      .set('@layout', resolve('src/layout'))
      .set('@static', resolve('src/static'))
    config.plugins.delete('prefetch') // 控制懒加载  vue-cli3默认全部import都一起加载了 所以要去
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .oneOf('inline')
      .resourceQuery(/inline/)
      .use('vue-svg-icon-loader')
      .loader('vue-svg-icon-loader')
      .end()
      .end()
      .oneOf('external')
      .use('file-loader')
      .loader('file-loader')
      .options({
        name: 'assets/[name].[hash:8].[ext]'
      })
  },
  css: {
    loaderOptions: {
      less: {
        modifyVars: antLessVarConfig,
        javascriptEnabled: true
      }
    }
  },
  devServer: {
    // hot:true,//如果有不支持热更新模块 就自动重新加载页面
    // development server port 8000
    port: 8000,
    host: '0.0.0.0',
    https: true, // https:{type:Boolean}
    open: true, // 配置自动启动浏览器

    proxy: {
      '/': {
        // target: 'http://10.82.253.98:12806',
        // target: 'http://10.13.195.101:12806',
        // target: 'http://10.82.253.38:12806',
        // target: 'http://10.13.195.52:12806',
        // target: 'http://10.13.11.86:12806',
        // hb
        // target: 'http://10.217.6.72:12806',
        // wj
        // target: 'http://10.217.6.195:12806',
        // hg
        // target: 'http://10.32.86.135:12806',
        // ls
        // target: 'http://10.32.86.101:12806',
        // lq
        //        target: 'http://10.32.86.246:12806',
        // mg
        // target: 'http://10.32.86.68:12806',
        // target:'http://10.82.252.192:12806',
        // target: 'http://10.82.253.11:12806',
        // target: 'http://10.13.11.149:12806', // 梁明龙
        // target: 'http://localhost:12806',
        // lx
        // target: 'http://10.13.195.104:12806',
          // kzs
          // target: 'http://10.82.252.41:12806',
        target: 'https://portal-ota-test-cn.byd.auto',
        // target: 'https://portal-ota-test-cn.denzacloud.com',
        //  target: 'https://portal-ota-dev-cn.byd.auto',
        // target: 'https://testdesay.kawa.site/bydotacd/', // cd
        // target: 'https://testdesay.kawa.site/bydota/', // 沙箱
        // target: 'https://otaportal-test-cn.byd.auto/', // uat
        changeOrigin: true,
        level: 'debug',
        pathRewrite: {
          '/baseApi': ''
        }
      }
    },
    headers: {
      version: 'local'
    }
  },
  lintOnSave: false,
  // babel-loader no-ignore node_modules/*
  transpileDependencies: [] // 通过这个选项可以显式转译一个依赖
}
